// 2025/3/8
// https://leetcode.cn/problems/swap-nodes-in-pairs/description/?envType=study-plan-v2&envId=top-100-liked
// 两两交换链表中的结点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    public:
        ListNode* swapPairs(ListNode* head) {
            ListNode phead(0, head), *pre = &phead;
            while(pre && pre->next && pre->next->next)
            {
                ListNode* tmp = pre->next;
                pre->next = pre->next->next;
                tmp->next = pre->next->next;
                pre->next->next = tmp;
                pre = pre->next->next;
            }
            return phead.next;
        }
    };